package org.wltea.analyzer.dic;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.cfg.DefaultConfig;

import java.sql.*;

import static org.wltea.analyzer.util.DbUtils.getLastUpdateTime;

/**
 * @author wwh
 * @date 2020年04月14日 9:47 上午
 */

public class Monitor implements Runnable {
    private final static Logger logger = LogManager.getLogger(Monitor.class);

    private static Timestamp extLastUpdateTime;
    private static Timestamp stopLastUpdateTime;
    private static Timestamp quantifierWordLastUpdateTime;


    public Monitor() {
    }

    @Override
    public void run() {

        Configuration cfg = DefaultConfig.getInstance();
        String extWordTable = cfg.getExtWordTable();
        Timestamp extDbLastUpdateTime = getLastUpdateTime(extWordTable);
        if (null != Monitor.extLastUpdateTime) {
            if (!Monitor.extLastUpdateTime.equals(extDbLastUpdateTime)) {
                Dictionary.sync();
                logger.info("检测扩展词典数据库表已修改，更新词典库");
                Monitor.extLastUpdateTime = extDbLastUpdateTime;
            }
        } else {
            Monitor.extLastUpdateTime = extDbLastUpdateTime;
        }
        String stopWordTable = cfg.getStopWordTable();
        Timestamp stopDbLastUpdateTime = getLastUpdateTime(stopWordTable);
        if (null != Monitor.stopLastUpdateTime) {
            if (!Monitor.stopLastUpdateTime.equals(stopDbLastUpdateTime)) {
                Dictionary.sync();
                logger.info("检测停用词典数据库表已修改，更新词典库");
                Monitor.stopLastUpdateTime = stopDbLastUpdateTime;
            }
        } else {
            Monitor.stopLastUpdateTime = stopDbLastUpdateTime;
        }
        String quantifierWordTable = cfg.getQuantifierWordTable();
        Timestamp quantifierWordDbLastUpdateTime = getLastUpdateTime(quantifierWordTable);
        if (null != Monitor.quantifierWordLastUpdateTime) {
            if (!Monitor.quantifierWordLastUpdateTime.equals(quantifierWordDbLastUpdateTime)) {
                Dictionary.sync();
                logger.info("检测到量词词典数据库表已修改，更新词典库");
                Monitor.quantifierWordLastUpdateTime = quantifierWordDbLastUpdateTime;
            }
        } else {
            Monitor.quantifierWordLastUpdateTime = quantifierWordDbLastUpdateTime;
        }

    }


}